<?php
namespace app\appadmin\controller\v1;
use app\app\Appbase;
use app\admin\model\SystemParamValue as ParamValue;
use app\appadmin\controller\v1\User;

class Index extends Appbase 
{
	
	/**
	* 首页
	* /appAdmin/v1/Index/index
	* 需要登录 
	* @is_need_login true
	*/
	public function index(){
		$shopId = request()->shopId;
		$identity = request()->identity;
		$uid = request()->userId;
		
		try {
			//TODO LOGO
	        $logo =  json_decode(getSystem("system_base_config_site"),true)['site_logo'] ?? '';
			//TODO 首页banner图
	        $banner = ParamValue::getSlides('buess_index_banner','shop_id_'.$shopId);
	        if( !$banner[0] ){
	        	$banner = ParamValue::getSlides('buess_index_banner','shop_id_0');
	        }
	        
	        if($identity==1){
	       		$data['turnover'] = model('order') 
	       			->whereTime('add_time', 'today')
	       			->where('is_pay',1)
	       			->where('status','>=',0)
	       			->sum('pay_price');
	       			
	       		//TODO 项目 
		        $proj = User::instance(true)->byProject(-1,-1);
	        	//TODO 生日 
	        	$birthday = User::instance(true)->birthday(-1,-1);
	        	//TODO 套餐到期 
	        	$kdq = User::instance(true)->tcdq(-1,-1);
	        	//TODO 余额
	        	$balance= User::instance(true)->yebz(-1,-1);	
	        }
	        if($identity==2){
	       		$data['turnover'] = model('order') 
	       			->whereTime('add_time', 'today')
	       			->where('is_pay',1)
	       			->where('status','>=',0)
	       			->where('shop_id',$shopId)
	       			->sum('pay_price');
	       		
	       		//TODO 项目 
		        $proj = User::instance(true)->byProject($shopId,-1);
	        	//TODO 生日 
	        	$birthday = User::instance(true)->birthday($shopId,-1);
	        	//TODO 套餐到期 
	        	$kdq = User::instance(true)->tcdq($shopId,-1);
	        	//TODO 余额
	        	$balance= User::instance(true)->yebz($shopId,-1);	
	        }
	        if($identity==3){
	       		$data['turnover'] = model('order') 
	       			->whereTime('add_time', 'today')
	       			->where('is_pay',1)
	       			->where('status','>=',0)
	       			->where('shop_id',$shopId)
	       			->where('shop_id',$uid)
	       			->sum('pay_price');
	       			
	       		//TODO 项目 
		        $proj = User::instance(true)->byProject($shopId,$uid);
	        	//TODO 生日 
	        	$birthday = User::instance(true)->birthday($shopId,$uid);
	        	//TODO 套餐到期 
	        	$kdq = User::instance(true)->tcdq($shopId,$uid);
	        	//TODO 余额
	        	$balance= User::instance(true)->yebz($shopId,$uid);	
	        }
	        
	        $data['remind'] = $proj + $birthday + $kdq +$balance;	
	        
			//返回
			return $this->ReSucess(compact('logo','banner','data') );	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 营业额
	* /appAdmin/v1/index/turnover
	* 需要登录 
	* @is_need_login true
	*/
	public function turnover($dates=1,$shop_id){
		$identity = request()->identity;
		if($identity==3){
			return $this->ReError('您无权查看！');	
		}
		try {
			
			$d1 = 'today';  $d2 = 'yesterday';
	        if($dates==2){  $d1 = 'month';  $d2 = 'last month'; }
	        if($dates==3){  $d1 = 'year';  $d2 = 'last year'; }
	        
	        //TODO 营业额 
        	$data['turnover'] = model('order') 
       			->whereTime('add_time', $d1)
       			->where('is_pay',1)
       			->where('status','>=',0)
       			->where('shop_id',$shop_id)
       			->sum('pay_price');
        	$data['last_turnover'] = model('order') 
       			->whereTime('add_time',$d2)
       			->where('is_pay',1)
       			->where('status','>=',0)
       			->where('shop_id',$shop_id)
       			->sum('pay_price');
       			
       		//TODO 会员
       		$data['member'] = model('order')->alias('a')
       			->leftjoin('user_vip b','a.uid = b.uid')
       			->whereTime('a.add_time', $d1)
       			->where('a.is_pay',1)
       			->where('a.status','>=',0)
       			->where('a.shop_id',$shop_id)
       			->wherenotnull('b.id')
       			->count();
       		$data['last_member'] = model('order')->alias('a')
       			->leftjoin('user_vip b','a.uid = b.uid')
       			->whereTime('a.add_time', $d2)
       			->where('a.is_pay',1)
       			->where('a.status','>=',0)
       			->where('a.shop_id',$shop_id)
       			->wherenotnull('b.id')
       			->count();
        	
        	//TODO 散客
        	$data['guest'] = model('order')->alias('a')
       			->leftjoin('user_vip b','a.uid = b.uid')
       			->whereTime('a.add_time', $d1)
       			->where('a.is_pay',1)
       			->where('a.status','>=',0)
       			->where('a.shop_id',$shop_id)
       			->wherenull('b.id')
       			->count();
       		$data['last_guest'] = model('order')->alias('a')
       			->leftjoin('user_vip b','a.uid = b.uid')
       			->whereTime('a.add_time', $d2)
       			->where('a.is_pay',1)
       			->where('a.status','>=',0)
       			->where('a.shop_id',$shop_id)
       			->wherenull('b.id')
       			->count();
        		
        	//TODO 总客户
        	$data['total_user'] = model('order')->alias('a')
       			->whereTime('a.add_time', $d1)
       			->where('a.is_pay',1)
       			->where('a.status','>=',0)
       			->where('a.shop_id',$shop_id)
       			->count();
       		$data['last_total_user'] = model('order')->alias('a')
       			->whereTime('a.add_time', $d2)
       			->where('a.is_pay',1)
       			->where('a.status','>=',0)
       			->where('a.shop_id',$shop_id)
       			->count();
        	
        	
	        //TODO 收入项目
	        $data['proj_list'] = [
		        [
		        	'name' => '服务项目',
		        	'amount'=> $y = model('order') 
			       			->whereTime('add_time', $d1)
			       			->where('is_pay',1)
			       			->where('status','>=',0)
			       			->where('type','in',"1,2")
			       			->where('shop_id',$shop_id)
			       			->sum('pay_price'),
		        	'ratio'=>  round( ($y <=0 ? 0 : $y / $data['turnover']) * 100 , 2). '%',
		        ],
			    [
		        	'name' => '普通产品',
		        	'amount'=> $y = model('order') 
			       			->whereTime('add_time', $d1)
			       			->where('is_pay',1)
			       			->where('status','>=',0)
			       			->where('type','not in',"1,2")
			       			->where('shop_id',$shop_id)
			       			->sum('pay_price'),
		        	'ratio'=>  round( ($y <=0 ? 0 : $y / $data['turnover']) * 100 , 2). '%',
		        ]
	        ];
	        
			//返回
			return $this->ReSucess(compact('logo','banner','data') );	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



